package com.etonedu.meals.dialog;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.etonedu.meals.R;
import com.etonedu.meals.enumeration.MessageEnum;
import com.etonedu.meals.machine.DeviceUtils;
import com.etonedu.meals.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

public class WindowDialog extends Dialog {

    private Context context;
    public WindowDialog(@NonNull Context context) {
        super(context, R.style.CustomDatePickerDialog1);
        this.context = context;
    }

    public WindowDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected WindowDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
    }

    private void init() {
        View view = setUpView(context);

        setContentView(view);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        DisplayMetrics d = context.getResources().getDisplayMetrics();

        Configuration mConfiguration = context.getResources().getConfiguration(); //获取设置的配置信息

        int ori = mConfiguration.orientation; //获取屏幕方向
        if (ori == mConfiguration.ORIENTATION_LANDSCAPE) {
            //横屏
            lp.width = (int) (d.widthPixels * 0.6);
            lp.height = (int) (d.heightPixels * 0.7);
        } else if (ori == mConfiguration.ORIENTATION_PORTRAIT) {
            //竖屏
            lp.width = (int) (d.widthPixels * 0.5);
            lp.height = (int) (d.heightPixels * 0.45);
        }

        dialogWindow.setAttributes(lp);

    }

    private View setUpView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_keyboard,null);
        Button btn_one = (Button) view.findViewById(R.id.btn_one);
        Button btn_two = (Button) view.findViewById(R.id.btn_two);
        Button btn_three = (Button) view.findViewById(R.id.btn_three);

        Button btn_four = (Button) view.findViewById(R.id.btn_four);
        Button btn_five = (Button) view.findViewById(R.id.btn_five);
        Button btn_six = (Button) view.findViewById(R.id.btn_six);

        Button btn_seven = (Button) view.findViewById(R.id.btn_seven);
        Button btn_eight = (Button) view.findViewById(R.id.btn_eight);
        Button btn_nine = (Button) view.findViewById(R.id.btn_nine);

        Button btn_temp = (Button) view.findViewById(R.id.btn_windows_temp);
        Button btn_zero = (Button) view.findViewById(R.id.btn_zero);
        Button btn_go_back = (Button) view.findViewById(R.id.btn_go_back);

        btn_one.setOnClickListener(new KeyBoardOnClickListener());
        btn_two.setOnClickListener(new KeyBoardOnClickListener());
        btn_three.setOnClickListener(new KeyBoardOnClickListener());

        btn_four.setOnClickListener(new KeyBoardOnClickListener());
        btn_five.setOnClickListener(new KeyBoardOnClickListener());
        btn_six.setOnClickListener(new KeyBoardOnClickListener());

        btn_seven.setOnClickListener(new KeyBoardOnClickListener());
        btn_eight.setOnClickListener(new KeyBoardOnClickListener());
        btn_nine.setOnClickListener(new KeyBoardOnClickListener());

        btn_zero.setOnClickListener(new KeyBoardOnClickListener());
        btn_go_back.setOnClickListener(new KeyBoardOnClickListener());

        String model = Build.MODEL;
        if ("rk3288".equals(model)) {

            DisplayMetrics dm = x.app().getResources().getDisplayMetrics();
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;

            btn_temp.setTextSize(20);
            btn_temp.setTextColor(Color.WHITE);
            btn_temp.setText("分辨率:"+screenWidth+"x"+screenHeight);
        } else {
            String result = Utils.do_exec("wm size");
            result = result.replace("Physical size","分辨率");
            btn_temp.setTextSize(20);
            btn_temp.setTextColor(Color.WHITE);
            btn_temp.setText(result);
        }


        return view;
    }

    private class KeyBoardOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            int id = v.getId();
            boolean isAppInstall = Utils.isAppInstalled(x.app(),"com.etonedu.autoboot");

            if(id == R.id.btn_one) {
//                    showToast("1");
//                    showInternetDialog(mActivity);

                HttpUrlDialog dialog = new HttpUrlDialog(context,R.style.CustomDialog);
                dialog.show();

                dismiss();
            } else if (id == R.id.btn_two) {
//                    showToast("2");
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                dismiss();
            } else if (id == R.id.btn_three) {
//                    showToast("3");
//                    Intent intent1 =  new Intent(Settings.ACTION_WIFI_SETTINGS);
//                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    mContext.startActivity(intent1);
                try {
                    int sdkVersion = Utils.getAndroidSDKVersion();
                    if (sdkVersion == Build.VERSION_CODES.LOLLIPOP_MR1) {
                        Intent intent1 = new Intent();
                        ComponentName comp = new ComponentName("com.android.rockchip", "com.android.rockchip.RockExplorer");
                        intent1.setComponent(comp);
                        intent1.setAction("android.intent.action.VIEW");
                        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent1);
                    } else if (sdkVersion == Build.VERSION_CODES.KITKAT) {
                        Intent intent1 = new Intent();
                        ComponentName comp = new ComponentName("com.softwinner.TvdFileManager", "com.softwinner.TvdFileManager.MainUI");
                        intent1.setComponent(comp);
                        intent1.setAction("android.intent.action.VIEW");
                        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent1);
                    }
                    dismiss();
                } catch (Exception e) {
                    Toast.makeText(x.app(), "没有安装文件浏览器，无法打开!", Toast.LENGTH_LONG).show();
                }

            } else if (id == R.id.btn_four) {
//                    showToast("4");
//                    Intent home = new Intent(Intent.ACTION_MAIN);
//
//                    home.addCategory(Intent.CATEGORY_HOME);
//                    home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    mContext.startActivity(home);
                Uri uri = Uri.parse("https://www.baidu.com");
                Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
                intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent2);
                dismiss();
            } else if (id == R.id.btn_five) {
                Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);

                mHomeIntent.addCategory(Intent.CATEGORY_HOME);
                mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                context.startActivity(mHomeIntent);
                dismiss();
//                    showToast("5");
            } else if (id == R.id.btn_six) {
//                    showToast("6");

                DeviceUtils.rebootMachine();
                DeviceUtils.reboot(x.app());
            } else if (id == R.id.btn_seven) {
//                    showToast("7");
//                        AppUpdate.devUpdateVersionInfo(mActivity);

                EventBus.getDefault().post(MessageEnum.NONE);
                dismiss();
            } else if (id == R.id.btn_eight) {
//                    showToast("8");
                Intent intentBoot = new Intent();
                intentBoot.setAction("com.etonedu.audoboot.receiver.updateVersion");
                x.app().sendBroadcast(intentBoot);
                dismiss();
            } else if (id == R.id.btn_nine) {
//                    showToast("9");
                Intent unintent = new Intent();
                unintent.setAction("com.etonedu.audoboot.receiver.uninstall");
                x.app().sendBroadcast(unintent);
//                    Utils.setEthernetEnabled(x.app(),false);
//                    Utils.closeEthernet(x.app());
//                    hidePopupWindow();
            } else if (id == R.id.btn_zero) {
//                    showToast("0");
//                    Utils.setEthernetEnabled(x.app(),true);
//                    Utils.openEthernet(x.app());
            } else if (id == R.id.btn_go_back) {
//                    showToast("返回");
                dismiss();
            }

//            hidePopupWindow();
        }
    }
}
